package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.SelfFleetEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.GoodsManageEvent;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: 熊丽
 * @date: 2021/10/11 10:41
 * @Description: 新增车队model
 */
public class AddProprietaryFleetViewModel extends AndroidViewModel {
    //车队提交数据
    public MutableLiveData<SelfFleetEntity> ldAddFleet = new MutableLiveData<>();
    public MutableLiveData<String> mlAddFleet = new MutableLiveData<>();

    public AddProprietaryFleetViewModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 待提交提交数据
     */
    public SelfFleetEntity submitData() {
        if (ldAddFleet.getValue() == null) {
            ldAddFleet.setValue(new SelfFleetEntity());
        }
        return ldAddFleet.getValue();
    }
    /**
     *  新增-编辑企业自营车队
     */
    public void setAddfleet(Context context) {
        DialogHelper.showProgressDialog(context, "加载中");
        Map<String, Object> map = new HashMap<>();
        if (!TextUtils.isEmpty(ldAddFleet.getValue().getMotorcadeNo())){
            map.put("motorcadeNo", ldAddFleet.getValue().getMotorcadeNo());
        }
        map.put("motorcadeName",ldAddFleet.getValue().getMotorcadeName());
        map.put("name", ldAddFleet.getValue().getName());
        map.put("telphone", ldAddFleet.getValue().getTelphone());
        if (ldAddFleet.getValue().getId()!=0){
            map.put("id",ldAddFleet.getValue().getId());
        }
        NewAPIManager.getInstance().getLogisticsAPI().subMitMotorcade(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                    if (response!=null) {
                        if ("200".equals(response.body().getCode())){
                            EventBus.getDefault().post(GoodsManageEvent.make().setMessageTag(EventConstant.EVENT_REFRESH_AFFILIATED_SUCCESS));
                            mlAddFleet.postValue(String.valueOf(response.body().getData()));
                        } else if (!TextUtils.isEmpty(response.body().getMsg())) {
                            ToastUtil.show(context, response.body().getMsg());
                        }
                    }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                ToastUtil.show(context, t.getMessage());
            }
        });
    }
}
